﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TGB.Core.Infrastructure;
using TGBTech.Core.Models.Services;

namespace TGBTech.WebAdmin.Controllers
{
    public class SoCaiTaiKhoanController : Controller
    {
        private TGBTechDb db = new TGBTechDb();
        //
        // GET: /SoCaiTaiKhoan/
        public ActionResult Index()
        {

            return View(db.SoCaiTaiKhoan.ToList());
        }

        //
        // GET: /SoCaiTaiKhoan/Details/5
        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /SoCaiTaiKhoan/Create
        public ActionResult Create()
        {
            var tk = db.TaiKhoan.Select(p => new
            {
                Id = p.Id,
                Display = p.Id + " | " + p.Name
            }).ToList();

            ViewBag.TaiKhoan = new SelectList(tk, "Id", "Display");
            return View();
        }

        //
        // POST: /SoCaiTaiKhoan/Create
        [HttpPost]
        public ActionResult Create(String TaiKhoan, DateTime? StartDate, DateTime? EndDate,
            Decimal? CoDauKy, Decimal? NoDauKy)
        {
            var tk = db.TaiKhoan.Select(p => new
            {
                Id = p.Id,
                Display = p.Id + " | " + p.Name
            }).ToList();
            ViewBag.TaiKhoan = new SelectList(tk, "Id", "Display");

            try
            {
                // TODO: Add insert logic here
                if (TaiKhoan == null || TaiKhoan == "" || !StartDate.HasValue || !EndDate.HasValue)
                {
                    return View();
                }

                //Goi services
                SoCaiTaiKhoanService service = new SoCaiTaiKhoanService();
                var model = service.createSoCaiTaiKhoan(TaiKhoan, StartDate.GetValueOrDefault(), EndDate.GetValueOrDefault(), CoDauKy.GetValueOrDefault(), NoDauKy.GetValueOrDefault());
                ViewBag.SoCaiTaiKhoan = model;
                


                var list = model.ListSoCaiTaiKhoanChiTiet;
                return View("Create", list);
            }
            catch
            {     
                return View();
            }
        }


        [HttpPost]
        public ActionResult Save(DateTime? Date)
        {
            // Save here

            return RedirectToAction("Index");
        }


        //
        // GET: /SoCaiTaiKhoan/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /SoCaiTaiKhoan/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
